<?php

namespace app\wanqing\controller;

use app\wanqing\model\WanqingFollowModel;
use app\wanqing\model\WanqingVirtualAddressRenewModel;
use cmf\controller\AdminBaseController;

class ListsController extends AdminBaseController
{
    function follow() {
        $where = [];
        $uid = cmf_get_current_admin_id();
        if ($uid != 1) {
            $users = implode(',', getUsersForRole($uid));
            $where['follow.uid'] = ['in', $users];
        }
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['user.user_nickname|cuser.user_nickname|follow.remark|company'] = ['like', "%$keyword%"];
        }
        if (input('start_time')) {
            $where['follow.create_time'] = ['egt',strtotime(input('start_time'))];
        }
        if (input('end_time')) {
            if(isset($where['follow.create_time'])) {
                $where['follow.create_time'] = ['between',[strtotime(input('start_time')),strtotime(input('end_time')) + 86399]];
            }else{
                $where['follow.create_time'] = ['elt',strtotime(input('end_time')) + 86399];
            }
        }

        $wanqingFollowModel = new WanqingFollowModel();
        $lists = $wanqingFollowModel
            ->alias('follow')
            ->join('__USER__ user','follow.uid = user.id')
            ->join('__WANQING_VIRTUAL_ADDRESS__ address','follow.pid = address.id')
            ->join('__USER__ cuser','address.uid = cuser.id')
            ->where($where)
            ->field('follow.*,user.user_nickname,address.end_time,address.company,cuser.user_nickname cusername')
            ->order('follow.id desc')
            ->paginate();

        $this->assign('lists',$lists);
        $this->assign('page',$lists->render());
        return $this->fetch();
    }

    function renew() {
        $where = [];
        $uid = cmf_get_current_admin_id();
        if ($uid != 1) {
            $users = implode(',', getUsersForRole($uid));
            $where['renew.uid'] = ['in', $users];
        }
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['user.user_nickname|cuser.user_nickname|renew.price|company'] = ['like', "%$keyword%"];
        }
        if (input('start_time')) {
            $where['renew.create_time'] = ['egt',strtotime(input('start_time'))];
        }
        if (input('end_time')) {
            if(isset($where['follow.create_time'])) {
                $where['renew.create_time'] = ['between',[strtotime(input('start_time')),strtotime(input('end_time')) + 86399]];
            }else{
                $where['renew.create_time'] = ['elt',strtotime(input('end_time')) + 86399];
            }
        }

        $wanqingVirtualAddressRenewModel = new WanqingVirtualAddressRenewModel();
        $lists = $wanqingVirtualAddressRenewModel
            ->alias('renew')
            ->join('__USER__ user','renew.uid = user.id')
            ->join('__WANQING_VIRTUAL_ADDRESS__ address','renew.vid = address.id')
            ->join('__USER__ cuser','address.uid = cuser.id')
            ->where($where)
            ->field('renew.*,user.user_nickname,address.end_time,address.company,cuser.user_nickname cusername')
            ->order('renew.id desc')
            ->paginate();

        $this->assign('lists',$lists);
        $this->assign('page',$lists->render());
        return $this->fetch();
    }
}